package com.huawei.domain;

import com.huawei.entity.Roles;
import com.huawei.entity.Users;
import org.springframework.security.core.GrantedAuthority;
import org.springframework.security.core.authority.SimpleGrantedAuthority;
import org.springframework.security.core.userdetails.UserDetails;

import java.util.Collection;
import java.util.List;
import java.util.stream.Collectors;

/***
 * @Author szay2005
 * @Slogan 元哥说Java
 */
public class AdminUserDetails implements UserDetails {

    // 用户信息
    Users users;

    List<Roles> roleList;
     

    public AdminUserDetails(Users umsAdmin, List<Roles> roleList) {
        this.users = umsAdmin;
        this.roleList = roleList;
    }

    public Users getUmsAdmin() {
        return users;
    }


    /**
     * 返回当前用户的角色信息
     * @return
     */
    @Override
    public Collection<? extends GrantedAuthority> getAuthorities() {
        //List<UmsRole> roleList = adminService.getRoleList(umsAdmin.getId());
        return  roleList.stream()
                .map(role->new SimpleGrantedAuthority(role.getName()))
                .collect(Collectors.toList());
    }

    @Override
    public String getPassword() {
        return users.getPassword();
    }

    @Override
    public String getUsername() {
        return users.getUsername();
    }

    @Override
    public boolean isAccountNonExpired() {
        return false;
    }

    @Override
    public boolean isAccountNonLocked() {
        return false;
    }

    @Override
    public boolean isCredentialsNonExpired() {
        return false;
    }

    @Override
    public boolean isEnabled() {
        return users.getStatus()=="1";
    }
}
